<?PHP

if(!function_exists('render_op_contentbody')){
	function render_op_contentbody($layout,$options){
		if (have_posts())
		{
			$post_number = 1;
			while(have_posts())
			{
				the_post();
				$divs_open = 0;
				$div_count = 1;
				$last_item = '';
				$even_odd = ($post_number & 1) ? "post_odd" : "post_even";
				echo "<div class=\"post post_number_{$post_number} $even_odd\" id=\"post-", get_the_ID(), '">';
				
				foreach($layout as $item)
				{
					switch ($item)
					{
						case 'title';
							echo '<h2><span><a href="'; the_permalink(); echo '" rel="bookmark" title="Permanent Link to ';
							the_title_attribute();
							echo '">'; the_title(); echo '</a></span></h2>';
							break;
						case 'author':
							$author_class = str_replace(' ','-',get_the_author());
							global $authordata; // required to lookup authors url
							$author_url = get_author_posts_url( $authordata->ID, $authordata->user_nicename );  // get link to author's posts
							echo '<div class="post_author"><div><span class="label">'.$options['author_pre'].'</span><a class="author_'.$author_class.'" href="'.$author_url.'">'; echo get_the_author(); echo '</a></div></div>';
							break;
						case 'time':
							echo '<div class="post_time"><div><span class="label">'.$options['time_pre'].'</span>'; the_time($options['time_format']); echo '</div></div>';
							break;
						case 'date':
							echo '<div class="post_date"><span>'; the_time($options['date_format']); echo '</span></div>';
							break;
						case 'post-body':
							echo '<div class="post_content">'; the_content($options['more_link']); echo '</div>';
							break;
						case 'post-excerpt':
							echo '<div class="post_excerpt">'; the_excerpt(); echo '</div>';
							break;
						case 'tags':
							if (has_tag()) {
								echo '<div class="post_tag"><div><span class="label">'.$options['tag_label'].'</span>'; the_tags($options['tag_before'], $options['tag_separator'], $options['tag_after']); echo '</div></div>';
							}
							break;
						case 'categories':
							echo '<div class="post_category"><div><span class="label">'.$options['category_label'].'</span>'; the_category($options['category_separator']); echo '</div></div>';
							break;
						case 'edit-link';
							echo '<div class="post_edit"><span>'; edit_post_link('Edit', '', ''); echo '</span></div>';
							break;
						case 'comments';
							echo '<div class="post_comments"><div>'; comments_popup_link($options['comment_link_none'], $options['comment_link_one'], $options['comment_link_multiple']); echo '</div></div>';
							break;
						case 'div':
							$divs_open++;
							echo '<div class="div-'.$div_count.'">';
							$div_count++;
							break;
						case 'div-close':
							if ($divs_open > 0) echo '</div>';
							$divs_open--;
							break;
						case 'thumbnail':
							global $post;	
							$img = OP_ImageSelection::get_image($post->ID,'thumbnail',FALSE);
							if($img != ''){
								echo '<div class="post_thumbnail"><span><a title="'; echo the_title(); echo '" href="'; the_permalink(); echo'"><img alt="'; echo the_title(); echo '" src="', $img, '" alt="" /></a></span></div>';
							}
							break;
						case 'op_content_hook':
							do_action('op_content_action', $last_item);
							break;
					}
					$last_item = $item;
				}
				if ($divs_open > 0){ // close all open divs
					 for($i=0; $i < $divs_open; $i++) echo '</div>';			 
				 }
				 
				if(is_single() || is_page()){
					echo '<div class="comment"><div>'; comments_template(); echo '</div></div>';
				}
	
				echo "</div>\n";
				$post_number += 1;
			}
		}
	}
}